package com.kedacom.freedomLabel.view.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.databinding.ActivityLabelViewerBinding;
import com.kedacom.freedomLabel.label.annotation.OriginalFontConfig;
import com.kedacom.freedomLabel.label.annotation.OriginalOptions;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.freedomLabel.viewmodel.LabelViewerViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


@ContentView(R.layout.activity_label_viewer)
@ViewModel(LabelViewerViewModel.class)
public class LabelViewerActivity extends BaseActivity<ActivityLabelViewerBinding, LabelViewerViewModel> {

    static final String TAG = "标签预览";

    static final String TEST_LABEL = "P,440,336\n" +
            "A,1,27,7,1,1,0,0,T,品    名：\n" +
            "A,7,78,7,1,1,0,0,T,规    格：,D,id1,11,0,15\n" +
            "A,9,118,7,1,1,0,0,T,批    号：,D,id2,22,0,15\n" +
            "A,9,204,7,1,1,0,0,T,装箱数量：,D,id4,20,0,8,T,PCS\n" +
            "A,4,245,8,1,1,0,0,T,重    量：,D,net,123.45,0,7,D,unit_w,kg,0,2\n" +
            "A,168,292,7,1,1,0,0,D,date,2016/10/12,0,10,T,  ,D,time,13:25,0,5\n" +
            "L,127,71,250,0,1,0\n" +
            "L,123,110,250,0,1,0\n" +
            "L,123,150,250,0,1,0\n" +
            "L,125,233,250,0,1,0\n" +
            "A,5,160,7,1,1,0,0,T,限    量：,D,id3,336778,0,12\n" +
            "L,126,192,250,0,1,0\n" +
            "L,125,280,250,0,1,0\n" +
            "TB,128,6,250,70,9,1,1,0,0,D,product,apple12中文1引文4测试5678901234特56789123456789,0,40";

    RelativeLayout container;

    // 1,27,7,1,1,0,0
    // X,Y,FONT,EX,EY,STYLE,ROTA

    /**
     * A,1,27,7,1,1,0,0
     * X,Y,FONT,EX,EY,STYLE,ROTA
     */
    public static class OriginalLabelTextColumn {

        public int x;
        public int y;
        public int size;
        public int scaleX;
        public int scaleY;
        public OriginalFontConfig.STYLE style;
        public OriginalFontConfig.ROTATION rotation;

        @Override
        public String toString() {
            return "OriginalLabelTextColumn{" +
                    "x=" + x +
                    ", y=" + y +
                    ", size=" + size +
                    ", scaleX=" + scaleX +
                    ", scaleY=" + scaleY +
                    ", style=" + style +
                    ", rotation=" + rotation +
                    '}';
        }
    }

    public static class OriginalLabelSize {
        public String width;
        public String height;

        @Override
        public String toString() {
            return "OriginalLabelSize{" +
                    "width='" + width + '\'' +
                    ", height='" + height + '\'' +
                    '}';
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        container = mBinding.container;

        AppExecutors.getInstance().queryIO().execute(() -> {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(TEST_LABEL.getBytes(StandardCharsets.UTF_8)),
                    StandardCharsets.UTF_8));
            try {
                List<String> labelConfigs = new ArrayList<>();
                String line;
                while ((line = br.readLine()) != null) {
                    labelConfigs.add(line);
                    LLog.debug(TAG, line);
                }
                getContainer(labelConfigs);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public void getContainer(List<String> labelConfigs) {
        if (labelConfigs != null && labelConfigs.size() > 0) {
            for (String line : labelConfigs) {
                int firstIndex = line.indexOf(',');
                byte[] commandBytes = new byte[firstIndex];
                System.arraycopy(line.getBytes(), 0, commandBytes, 0, firstIndex);
                int command = OriginalOptions.bytesToInt(commandBytes);
                switch (command) {
                    case OriginalOptions.STX_SIZE:
                        LLog.info(TAG, "执行标签尺寸解析");
                        OriginalLabelSize labelSize = convertOriginalLabelSize(line);
                        LLog.info(TAG, "标签尺寸解析成功 \n" + labelSize.toString());
                        break;
                    case OriginalOptions.STX_TEXT:
                        int indexOfT = line.indexOf('T');
                        String fontString = line.substring(0, indexOfT - 1);
                        LLog.info(TAG, "执行单行文本格式解析 " + fontString);
                        OriginalLabelTextColumn column = convertOriginalTextColumn(fontString);
                        LLog.info(TAG, "执行单行文本格式解析成功 \n" + column.toString());
                        String dataString = line.substring(indexOfT);
                        LLog.info(TAG, "执行单行文本内容解析 " + dataString);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * A,1,27,7,1,1,0,0
     * X,Y,FONT,EX,EY,STYLE,ROTA
     */
    public OriginalLabelTextColumn convertOriginalTextColumn(String line) {
        String[] strings = line.split(",");
        if (strings.length < 8) {
            return null;
        }
        OriginalLabelTextColumn column = new OriginalLabelTextColumn();
        column.x = Integer.parseInt(strings[1]);
        column.y = Integer.parseInt(strings[2]);
        column.size = Integer.parseInt(strings[3]);
        column.scaleX = Integer.parseInt(strings[4]);
        column.scaleY = Integer.parseInt(strings[5]);
        int styleCode = Integer.parseInt(strings[6]);
        column.style = OriginalOptions.getOriginalFontStyle(styleCode);
        int rotationCode = Integer.parseInt(strings[7]);
        column.rotation = OriginalOptions.getOriginalFontRotation(rotationCode);
        return column;
    }

    public OriginalLabelSize convertOriginalLabelSize(String line) {
        String[] strings = line.split(",");
        if (strings.length < 3) {
            return null;
        }
        OriginalLabelSize labelSize = new OriginalLabelSize();
        labelSize.width = strings[1];
        labelSize.height = strings[2];
        return labelSize;
    }
}